package dymnd.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

import javax.swing.BorderFactory;

public class SeekBar extends javax.swing.JPanel implements Serializable{
	 
    private int x_inset = 1;
    private int y_inset = 1;
    
    private Color seekBarColor = Color.green;
    
    private int xPercent = 100;
    
    private boolean releasedFlag = true;
    
    private Rectangle scrollArea;
    
    private double value = 100;
    
    private Image backg;
    
    @Override 
    public void paintComponent(Graphics arg0)  {
        checkConsistentScrollArea();
		//make sure our back buffer is acceptable.
		if(backg == null || backg.getWidth(this) != getWidth() || backg.getHeight(this) != getHeight())
			backg = createImage(getWidth(), getHeight());
		
		Graphics2D g = (Graphics2D) backg.getGraphics();
		
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		//paint the background
		//g.setComposite(AlphaComposite.Src);
		g.setColor(getBackground());
		g.fill(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
		
		//g.setComposite(AlphaComposite.SrcAtop);
		//draw the seek bar
		g.setColor(seekBarColor);
		g.fill(new Rectangle2D.Double(scrollArea.x, scrollArea.y, scrollArea.width*(xPercent/100.0), scrollArea.height));
		
		//draw the selected percentage
		if(!releasedFlag)	{
			//g.setColor(getForeground());
			//g.drawString("" + xPercent + "%", getWidth()/2, getHeight()/2);
		}
		
		arg0.drawImage(backg, 0, 0, null);
		g.dispose();
        
    }
    
    private void updateXPercent(int x)	{
        if(x - x_inset > scrollArea.width) {
                x = scrollArea.width+x_inset;
        }
		
        if(x - x_inset < x_inset)
            x = x_inset;
		
        xPercent = (int) (((double)(x - scrollArea.x))/((double)scrollArea.width)*100.0);
        repaint();
		
    }
    
    
    private Rectangle calculateScrollArea()	{
		return new Rectangle(x_inset, y_inset, getWidth()- x_inset*2, getHeight() - y_inset*2);
    }
    
    private void checkConsistentScrollArea()	{
		if(scrollArea == null || scrollArea.width != getWidth()-x_inset*2 || scrollArea.height != getHeight()-y_inset*2)	{
			scrollArea = calculateScrollArea();
		}
    }
    
    /**
     * Returns the selected percent of the value specified in the constructor.
     * @return
     */
    public double getValue()	{
            return (xPercent/100.0)*value;
    }

    /**
     * Returns the current selection's percent.
     * @return
     */
    public double getPercentAsDecimal()	{
            return (xPercent/100.0);
    }

    public Color getSeekBarColor() {
        return seekBarColor;
    }

    public void setSeekBarColor(Color seekBarColor) {
        this.seekBarColor = seekBarColor;
    }

    public int getPercent() {
        return xPercent;
    }

    public void setPercent(int xPercent) {
        this.xPercent = xPercent;
    }

    public int getX_inset() {
        return x_inset;
    }

    public void setX_inset(int x_inset) {
        this.x_inset = x_inset;
    }

    public int getY_inset() {
        return y_inset;
    }

    public void setY_inset(int y_inset) {
        this.y_inset = y_inset;
    }
    
    
    
        
        
    
    /**
     * @param value - the value the seek bar is finding a percentage of
     */
    public SeekBar(double value) {
        this.value = value;
		
		
		scrollArea = calculateScrollArea();
		
		addMouseListener(new MouseAdapter()	{
                        @Override
			public void mouseReleased(MouseEvent e)	{
				checkConsistentScrollArea();
				updateXPercent(e.getX());
				//System.out.println(xPercent);
				releasedFlag = true;
			}
			@Override
			public void mousePressed(MouseEvent e)	{
				checkConsistentScrollArea();
				updateXPercent(e.getX());
				releasedFlag = false;
			}
			
		});
	
		addMouseMotionListener(new MouseMotionAdapter()	{
                        @Override
			public void mouseDragged(MouseEvent e)	{
				checkConsistentScrollArea();
				updateXPercent(e.getX());
				releasedFlag = false;
			}
		});
		
		setBorder(BorderFactory.createEtchedBorder(Color.lightGray, Color.darkGray));
		setBackground(ThemeConstants.THEME_SEEKER_BACKGROUND_COLOR);
		setSeekBarColor(ThemeConstants.THEME_SEEKER_BAR_COLOR);
    }
    
     public SeekBar() {
        
		scrollArea = calculateScrollArea();
		
		addMouseListener(new MouseAdapter()	{
                        @Override
			public void mouseReleased(MouseEvent e)	{
				checkConsistentScrollArea();
				updateXPercent(e.getX());
				//System.out.println(xPercent);
				releasedFlag = true;
			}
			@Override
			public void mousePressed(MouseEvent e)	{
				checkConsistentScrollArea();
				updateXPercent(e.getX());
				releasedFlag = false;
			}
			
		});
	
		addMouseMotionListener(new MouseMotionAdapter()	{
                        @Override
			public void mouseDragged(MouseEvent e)	{
				checkConsistentScrollArea();
				updateXPercent(e.getX());
				releasedFlag = false;
			}
		});
		
		setBorder(BorderFactory.createEtchedBorder(Color.lightGray, Color.darkGray));
        setBackground(ThemeConstants.THEME_SEEKER_BACKGROUND_COLOR);
    }

}
